package com.soonfor.evaluate.activity.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.ui.install.BuildDetailActivity;
import com.estronger.shopmanage.ui.measure.MeasureDetailActivity;
import com.estronger.shopmanage.ui.othertask.activity.OtherTaskDetailActivity;
import com.estronger.shopmanage.ui.send.SendDetailActivity;
import com.soonfor.evaluate.base.EvalBaseFragment;
import com.soonfor.evaluate.bean.EvaluateListBean;
import com.soonfor.evaluate.http.api.Request;
import com.soonfor.evaluate.presenter.Evaluate_CustomersToMePresenter;
import com.soonfor.evaluate.presenter.IEvaluate_CustomerToMeView;
import com.soonfor.evaluate.activity.Evaluate_CustomersToMeDetailActivity;
import com.soonfor.evaluate.adapter.Evaluate_CustomersToMeAdapter;
import com.soonfor.evaluate.tools.Tokens;

/**
 * 作者：DC-DingYG on 2018-10-17 15:32
 * 邮箱：dingyg012655@126.com
 * 已评价Fragment
 */
public class Evaluated_CustomersToMeFragment extends EvalBaseFragment<Evaluate_CustomersToMePresenter> implements IEvaluate_CustomerToMeView {

    Unbinder unbinder;
    Activity mActivity;
    @BindView(R.id.recyView)
    RecyclerView mRecyclerView;
    private LinearLayoutManager mLayoutManager;
    private Evaluate_CustomersToMeAdapter etmAdpter;
    private List<EvaluateListBean> etmList;
    private int currentPage = 1;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        etmList = new ArrayList<>();
        mLayoutManager = new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false);
        etmAdpter = new Evaluate_CustomersToMeAdapter(mActivity, 1, adapterListener);
        // 设置布局管理器
        mRecyclerView.setLayoutManager(mLayoutManager);
        // 设置adapter
        mRecyclerView.setAdapter(etmAdpter);
        return rootView;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_mine_evaluate;
    }

    @Override
    protected void initPresenter() {
        presenter = new Evaluate_CustomersToMePresenter(this, mActivity);
    }

    @Override
    protected void initViews() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeRefresh.autoRefresh();
            }
        }, 500);
    }

    @Override
    protected void updateViews(boolean isRefresh) {}

    @Override
    public void RefreshData(boolean isRefresh) {
        super.RefreshData(isRefresh);
        currentPage = 1;
        getList();
    }

    @Override
    protected void loadmoredata() {
        currentPage++;
        getList();
    }
    //请求列表
    private void getList(){
        presenter.getEvaluatedList(mActivity,1, currentPage, Request.Evaluate.GET_EVALUATETOMELIST_Y);
    }
    @Override
    public void showYList(boolean isSuccess, List<EvaluateListBean> beans, String msg) {
        if (isSuccess) {
            if (currentPage <= 1) {
                if (etmList == null) etmList = new ArrayList<>();
                else if (etmList.size() > 0) etmList.clear();
            }
            if (beans.size() > 0) {
                etmList.addAll(beans);
            }
            if (etmList.size() > 0) {
                showDataToView(null);
            } else {
                showNoDataHint(msg);
            }
            etmAdpter.notifyDataSetChanged(etmList);
        } else {
            if (currentPage == 1) showNoDataHint(msg);
        }
    }

    @Override
    public void showNList(boolean isSuccess, List<EvaluateListBean> beans, String msg) {

    }
    private View.OnClickListener adapterListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = (int) v.getTag();
            switch (v.getId()) {
                case R.id.tvfServicesAvailable:
                    EvaluateListBean ectBean = etmList.get(pos);
                    Bundle bundle = new Bundle();
                    switch (ectBean.getFappraiseoritype()) {
                        case 0://其它任务
                            bundle.putSerializable(Tokens.OtherTask.OT_ITEMSKIPDETAIL_ITEM, presenter.convertOtherTaskBean(ectBean));
                            bundle.putInt(Tokens.OtherTask.OT_ITEMSKIPDETAIL_STATUS, 3);
                            startNewAct(OtherTaskDetailActivity.class, bundle);
                            break;
                        case 1://测量任务
                            Intent intent1 = new Intent(mActivity, MeasureDetailActivity.class);
                            intent1.putExtra("isComplesh", true);
                            intent1.putExtra("measureid", ectBean.getFplantaskid());
                            mActivity.startActivity(intent1);
                            break;
                        case 5://送货任务
                            Intent intent5 = new Intent(mActivity, SendDetailActivity.class);
                            intent5.putExtra("isComplesh", true);
                            intent5.putExtra("dlvid", ectBean.getFplantaskid());
                            mActivity.startActivity(intent5);
                            break;
                        case 6://安装任务
                            Intent intent6 = new Intent(mActivity, BuildDetailActivity.class);
                            intent6.putExtra("isComplesh", true);
                            intent6.putExtra("iftestimony", false);
                            intent6.putExtra("dlvid", ectBean.getFplantaskid());
                            mActivity.startActivity(intent6);
                            break;
                    }
                    break;
                case R.id.llfItem:
                    Intent intent2 = new Intent(mActivity, Evaluate_CustomersToMeDetailActivity.class);
                    intent2.putExtra("EvaluateListBean", etmList.get(pos));
                    intent2.putExtra("List_Position", pos);
                    mActivity.startActivity(intent2);
                    break;
            }
        }
    };

    @Override
    public void showLoadingDialog() {
        if (mLoadingDialog != null && !mLoadingDialog.isShowing())
            mLoadingDialog.show();
    }

    @Override
    public void closeLoadingDialog() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing())
            mLoadingDialog.dismiss();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
